#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "stack.h"

int bba(char* string)
{
	int i;
	stack STACK;
	stack_create(&STACK);
	for(i=0; i<strlen(string); i++)
	{
		if(string[i]=='(')
		{
				stack_push(&STACK, (pointer)'('); 
		}
		if(string[i]=='{')
		{
			stack_push(&STACK, (pointer)'{'); 
		}
		if(string[i]=='[')
		{
			stack_push(&STACK, (pointer)'['); 
		}
		if(string[i]=='<')
		{ 
			stack_push(&STACK, (pointer)'<'); 
		}
		if(string[i]==')')
		{
			if(stack_peek(&STACK)==(pointer)0)
			{
				stack_destroy(&STACK);
				return 0;
			}
			if(stack_peek(&STACK)==(pointer)'(')
			{
				stack_pop(&STACK);
			}
			else
			{
				stack_destroy(&STACK);
				return 0;
			}
		}
		if(string[i]==']')
		{
			if(stack_peek(&STACK)==(pointer)0)
			{
				stack_destroy(&STACK);
				return 0;
			}
			if(stack_peek(&STACK)==(pointer)'[')
			{
				stack_pop(&STACK);
			}
			else
			{
				stack_destroy(&STACK);
				return 0;
			}
		}
		if(string[i]=='}')
		{
			if(stack_peek(&STACK)==(pointer)0)
			{
				stack_destroy(&STACK);
				return 0;
			}
			if(stack_peek(&STACK)==(pointer)'{')
			{
				stack_pop(&STACK);
			}
			else
			{
				stack_destroy(&STACK);
				return 0;
			}
		}
		if(string[i]=='>')
		{
			if(stack_peek(&STACK)==(pointer)0)
			{
				stack_destroy(&STACK);
				return 0;
			}
			if(stack_peek(&STACK)==(pointer)'<')
			{
				stack_pop(&STACK);
			}
			else
			{
				stack_destroy(&STACK);
				return 0;
			}
		}
	}
	stack_destroy(&STACK);
	return 1;
}
void main()
{
	char string[1024];
	assert(bba("{()[()]}<<<>>>") == 1);
	assert(bba("{}()<({}<>)>") == 1);
	assert(bba("[(])") == 0);
	assert(bba("{{[]]}}") == 0);
	printf("Input string: \n");
	scanf("%s", &string);
	if(bba(string)==0)
	{
		printf("Brackets aren't balanced.\n");
	}
	else
	{
		printf("Brackets are balanced.\n");
	}
	system("PAUSE");
}